package com.realtyManage.springboot.Controller;

import com.alibaba.fastjson.JSON;
import com.realtyManage.springboot.Model.Do.Area;
import com.realtyManage.springboot.Model.Do.SysLog;
import com.realtyManage.springboot.Model.Vo.BaseEntity;
import com.realtyManage.springboot.Model.Vo.RegEntity;
import com.realtyManage.springboot.Model.Vo.SysLogEntity;
import com.realtyManage.springboot.Service.AreaService;
import com.realtyManage.springboot.Service.SysLogService;
import com.realtyManage.springboot.Utils.SysExecRsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/")
public class AreaController {

    @Autowired
    private AreaService areaService;

    /**
     * 查询城市信息
     * @param entity
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showAreaList",method = RequestMethod.GET)
    private String showAreaList(BaseEntity entity, HttpServletRequest request) {
        Page<Area> areaList = areaService.findPagedByWhereClause(entity);
        return JSON.toJSONString(areaList);
    }


    /**
     * 物理删除城市信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteArea",method = RequestMethod.POST)
    @ResponseBody
    private String deleteArea(String areaId, HttpServletRequest request) {
        Integer code = -1;
        try{
            if (areaId != null || areaId != ""){
                if (areaService.deleteArea(areaId) > 0){
                    code = 1;
                }else{
                    code = -1;
                }
            }
        }catch (Exception e){
            e.getMessage();
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 新增城市信息
     * @param area
     * @param request
     * @return
     */
    @RequestMapping(value = "/addArea",method = RequestMethod.POST)
    @ResponseBody
    private String addArea(Area area, HttpServletRequest request) {
        Integer code = -1;
        if (areaService.save(area).getId() != null){
            code = 1;
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }



}
